package com.example.pkmedical.View;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.pkmedical.R;


/**
 * Created by Administrator on 2020/img_bottom_fbzl_on/19.
 */

public class UnlockView extends FrameLayout {

    private ImageView ivSlide, ivInitial, ivMove;
    private Context context;
    int startKeyLeft, startKeyRight;
    private Boolean isfirst = true;
    private Float startX;
    private LinearLayout.LayoutParams llParams;
    private RelativeLayout.LayoutParams rlParams;
    private OnUnlockListener onUnlockListener;

    public UnlockView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.view_unlock, this);
        ivSlide = findViewById(R.id.iv_slide);
        ivInitial = findViewById(R.id.iv_initial);
        ivMove = findViewById(R.id.iv_move);
        ivSlide.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        ivInitial.setVisibility(VISIBLE);
                        ivMove.setVisibility(VISIBLE);
                        startX = motionEvent.getX();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int offsetX = (int) (motionEvent.getX() - startX);
                        if (ivSlide.getLeft() + offsetX < 2) {

                        } else if (ivSlide.getRight() + offsetX >= getMeasuredWidth() - 2) {
                            rlParams = (RelativeLayout.LayoutParams) ivSlide.getLayoutParams();
                            rlParams.leftMargin = getMeasuredWidth() - 2 - ivSlide.getMeasuredWidth();
                            ivSlide.setLayoutParams(rlParams);
                            llParams = (LinearLayout.LayoutParams) ivMove.getLayoutParams();
                            llParams.width = ivSlide.getLeft();
                            ivMove.setLayoutParams(llParams);
                            if (onUnlockListener != null) {
                                onUnlockListener.onOpen();
                            }
                        } else {
                            rlParams = (RelativeLayout.LayoutParams) ivSlide.getLayoutParams();
                            rlParams.leftMargin = ivSlide.getLeft() + offsetX;
                            ivSlide.setLayoutParams(rlParams);
                            llParams = (LinearLayout.LayoutParams) ivMove.getLayoutParams();
                            llParams.width = ivSlide.getLeft() + offsetX;
                            ivMove.setLayoutParams(llParams);
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        ivInitial.setVisibility(INVISIBLE);
                        llParams = (LinearLayout.LayoutParams) ivMove.getLayoutParams();
                        llParams.width = 2;
                        ivMove.setLayoutParams(llParams);

                        rlParams = (RelativeLayout.LayoutParams) ivSlide.getLayoutParams();
                        rlParams.leftMargin = 2;
                        ivSlide.setLayoutParams(rlParams);
                        ivMove.setVisibility(INVISIBLE);
                        break;

                }
                return true;
            }
        });

    }

    public void setOnUnlockListener(OnUnlockListener onUnlockListener) {
        this.onUnlockListener = onUnlockListener;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (isfirst) {
            startKeyLeft = ivSlide.getLeft();
            startKeyRight = ivSlide.getRight();
            isfirst = false;
        }
    }

    public interface OnUnlockListener {
        void onOpen();
    }
}
